package com.vehicle.common.util.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by yantingbin on 2017/7/11.
 */
import com.google.gson.internal.$Gson$Types;
import com.google.gson.internal.$Gson$Preconditions;

public class GenericHelper {

    private GenericHelper(){
        throw  new AssertionError();
    }

    public static <T> Class<T> getClz(Class<?> subclass ,int index){

        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }

        ParameterizedType parameterized = (ParameterizedType) superclass;

        //ParameterizedType type = ParameterizedTypeImpl.get(subclass);

        Type[] types= parameterized.getActualTypeArguments();

        if(types.length-1>index||index<-1){
            throw new IndexOutOfBoundsException("Index is out of range.");
        }

        return (Class<T>)$Gson$Types.getRawType(types[index]);
    }
}
